package cn.iocoder.devlop.module.annual.report.subcapital.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.subcapital.controller.vo.SubcapitalCreateReqVO;
import cn.iocoder.devlop.module.annual.report.subcapital.controller.vo.SubcapitalOverwriteVO;
import cn.iocoder.devlop.module.annual.report.subcapital.controller.vo.SubcapitalUpdateReqVO;
import cn.iocoder.devlop.module.annual.report.subcapital.convert.SubcapitalConvert;
import cn.iocoder.devlop.module.annual.report.subcapital.dal.SubcapitalMapper;
import cn.iocoder.devlop.module.annual.report.subcapital.dal.dataobject.SubcapitalDO;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.devlop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.devlop.module.annual.report.enums.ErrorCodeConstants.SUBCAPITAL_NOT_EXISTS;

/**
 * 年报股东及出资 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SubcapitalServiceImpl implements SubcapitalService {

    @Resource
    private SubcapitalMapper subcapitalMapper;

    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;

    @Resource
    private BaseinfoService baseinfoService;

    @Override
    public String createSubcapital(SubcapitalCreateReqVO createReqVO) {
        // 插入
        SubcapitalDO subcapital = SubcapitalConvert.INSTANCE.convert(createReqVO);
        subcapitalMapper.insert(subcapital);
        // 返回
        return subcapital.getInvid();
    }

    @Override
    public void updateSubcapital(SubcapitalUpdateReqVO updateReqVO) {
        // 校验存在
        validateSubcapitalExists(updateReqVO.getInvid());
        // 更新
        SubcapitalDO updateObj = SubcapitalConvert.INSTANCE.convert(updateReqVO);
        subcapitalMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubcapital(String id) {
        // 校验存在
        validateSubcapitalExists(id);
        // 删除
        subcapitalMapper.deleteById(id);
    }

    private void validateSubcapitalExists(String id) {
        if (subcapitalMapper.selectById(id) == null) {
            throw exception(SUBCAPITAL_NOT_EXISTS);
        }
    }

    @Override
    public SubcapitalDO getSubcapital(String id) {
        return subcapitalMapper.selectById(id);
    }

    @Override
    public List<SubcapitalDO> getSubcapitalList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return subcapitalMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteSubcapitalAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<SubcapitalDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(SubcapitalDO::getAncheid,ancheid);
        subcapitalMapper.delete(wrapperX);
    }

    @Override
    public void createSubcapitalByList(String regno, String ancheyear, List<SubcapitalCreateReqVO> subcapitalList) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfo.getEnttype());
        if (CollUtil.isNotEmpty(subcapitalList)) {
            List<SubcapitalDO> subcapitalDOS = SubcapitalConvert.INSTANCE.convertList01(subcapitalList);
            for (SubcapitalDO subcapital : subcapitalDOS) {
                subcapital.setAncheid(ancheid);
                subcapital.setInvid(IdUtil.simpleUUID());
                subcapital.setEntclassify(entclassify);
            }
            subcapitalMapper.insertBatch(subcapitalDOS);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteSubcapital(SubcapitalOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<SubcapitalDO> oldSubcapitalList = getSubcapitalList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<SubcapitalCreateReqVO> newAlterstockinfoList = updateReqVO.getSubcapitalList();
                List<SubcapitalCreateReqVO> oldreqs = SubcapitalConvert.INSTANCE.convertList3(oldSubcapitalList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteSubcapitalAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createSubcapitalByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getSubcapitalList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }


}
